{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 数据预处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0.58275961  0.54368471  0.25242627 ... -0.01263333 -0.01606076\n",
      "  -0.04140337]\n",
      " [ 0.6418702   0.57077255  0.21357176 ...  0.00957151 -0.00398529\n",
      "  -0.00203014]\n",
      " [ 0.60369804  0.57679765  0.28219647 ... -0.00465519 -0.00233666\n",
      "  -0.00954887]\n",
      " ...\n",
      " [ 0.44123333  0.51526902  0.19537255 ... -0.00701969 -0.00491445\n",
      "   0.00924045]\n",
      " [ 0.44364431  0.5259898   0.18965569 ... -0.00648428 -0.00700152\n",
      "   0.00893731]\n",
      " [ 0.58383529  0.58097294  0.35063922 ... -0.007575   -0.0089826\n",
      "  -0.01612694]]\n",
      "[1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
      " 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1. 1.\n",
      " 1. 1. 1. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2.\n",
      " 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 2. 3.\n",
      " 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3.\n",
      " 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3.\n",
      " 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3. 3.\n",
      " 3. 3. 3. 3. 3. 4. 4. 4. 4. 4. 4. 4. 4. 4. 4. 4. 4. 4. 4. 4. 4. 4. 4. 4.\n",
      " 4. 4. 4. 4. 4. 5. 5. 5. 5. 5. 5.]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import os,re\n",
    "from PIL import Image\n",
    "\n",
    "# 图像切割及特征提取\n",
    "path = '../data/images/'  # 图片所在路径\n",
    "# 自定义获取图片名称函数\n",
    "def getImgNames(path=path):\n",
    "    '''\n",
    "    获取指定路径中所有图片的名称\n",
    "    :param path: 指定的路径\n",
    "    :return: 名称列表\n",
    "    '''\n",
    "    filenames = os.listdir(path)\n",
    "    imgNames = []\n",
    "    for i in filenames:\n",
    "        if re.findall('^\\d_\\d+\\.jpg$', i) != []:\n",
    "            imgNames.append(i)\n",
    "    return imgNames\n",
    "\n",
    "# 自定义获取三阶颜色矩函数\n",
    "def Var(data=None):\n",
    "    '''\n",
    "    获取给定像素值矩阵的三阶颜色矩\n",
    "    :param data: 给定的像素值矩阵\n",
    "    :return: 对应的三阶颜色矩\n",
    "    '''\n",
    "    x = np.mean((data-data.mean())**3)\n",
    "    return np.sign(x)*abs(x)**(1/3)\n",
    "\n",
    "# 批量处理图片数据\n",
    "imgNames = getImgNames(path=path)  # 获取所有图片名称\n",
    "n = len(imgNames)        # 图片张数\n",
    "data = np.zeros([n, 9])  # 用来装样本自变量\n",
    "labels = np.zeros([n])   # 用来放样本标签\n",
    "\n",
    "for i in range(n):\n",
    "    img = Image.open(path+imgNames[i])  # 读取图片\n",
    "    M,N = img.size  # 图片像素的尺寸\n",
    "    img = img.crop((M/2-50,N/2-50,M/2+50,N/2+50))    # 图片切割\n",
    "    r,g,b = img.split()     # 将图片分割成三通道\n",
    "    rd = np.asarray(r)/255  # 转化成数组数据\n",
    "    gd = np.asarray(g)/255\n",
    "    bd = np.asarray(b)/255\n",
    "\n",
    "    data[i,0] = rd.mean()  # 一阶颜色矩\n",
    "    data[i,1] = gd.mean()\n",
    "    data[i,2] = bd.mean()\n",
    "\n",
    "    data[i,3] = rd.std()   # 二阶颜色矩\n",
    "    data[i,4] = gd.std()\n",
    "    data[i,5] = bd.std()\n",
    "\n",
    "    data[i,6] = Var(rd)    # 三阶颜色矩\n",
    "    data[i,7] = Var(gd)\n",
    "    data[i,8] = Var(bd)\n",
    "\n",
    "    labels[i] = imgNames[i][0]  # 样本标签\n",
    "\n",
    "print(data)\n",
    "print(labels)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 模型构建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "# 数据拆分,训练集、测试集\n",
    "data_tr,data_te,label_tr,label_te = train_test_split(data,labels,test_size=0.2,random_state=10)\n",
    "\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "# 模型训练\n",
    "model = DecisionTreeClassifier(random_state=5).fit(data_tr, label_tr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 模型评价"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "混淆矩阵为：\n",
      " [[ 5  1  5  0]\n",
      " [ 3  8  0  0]\n",
      " [ 2  0 12  0]\n",
      " [ 0  1  0  4]]\n",
      "准确率为：\n",
      " 0.7073170731707317\n"
     ]
    }
   ],
   "source": [
    "# 水质评价\n",
    "from sklearn.metrics import confusion_matrix\n",
    "pre_te = model.predict(data_te)\n",
    "# 混淆矩阵\n",
    "cm_te = confusion_matrix(label_te,pre_te)\n",
    "print('混淆矩阵为：\\n', cm_te)\n",
    "from sklearn.metrics import accuracy_score\n",
    "# 准确率\n",
    "print('准确率为：\\n', accuracy_score(label_te,pre_te))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAA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OkvaQtDPlqzQ/R/kDZZcK/+lmH+NbK1gtW6fZrRseoHxJ/KeAV7N77EP5B1i2Ax7MbmneDXgI2AZ4OSJeAMhuoDV2BW3sAxwN5btWAgslbdhimf2zx2PZ656UPwDWA26LiPezNu5Yrb/WbCUc+FbLPshuG7xcFuqLmk+ifA/5b7RYbkcqd8tbAT+JiF+1aOPUCrZhtlIe0rHUTQG+KGkggKR1JW1N+Y6TW0jaMlvuG628/z7gxOy9XSStD7xLee99mXuBbzU7NtBPUm/gfuBgSetIWo/y8JFZbhz4lrSImA8cC/xO0pOUPwC2iYgPKQ/hTMwO2r7ayipOAfaWNAOYDmwfEW9SHiJ6StIlEfFn4LfAQ9lyfwTWy37mcQLwOOXfBnggtz/UDN8t08wsGd7DNzNLhAPfzCwRDnwzs0Q48M3MEuHANzNLhAPfzCwRDnwzs0Q48M3MEvF/NuHRsRxPiOsAAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x360 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import numpy as np\n",
    "from sklearn.metrics import confusion_matrix\n",
    "\n",
    "# 计算混淆矩阵\n",
    "cm_te = confusion_matrix(label_te, pre_te)\n",
    "\n",
    "# 创建热力图\n",
    "plt.figure(figsize=(6, 5))\n",
    "sns.heatmap(cm_te, annot=True, fmt='d', cmap='Blues', cbar=False, \n",
    "            xticklabels=np.unique(label_te), yticklabels=np.unique(label_te))\n",
    "\n",
    "# 添加标题和标签\n",
    "plt.title('Confusion Matrix')\n",
    "plt.xlabel('Predicted')\n",
    "plt.ylabel('True')\n",
    "\n",
    "# 显示图形\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "分类报告为：\n",
      "               precision    recall  f1-score   support\n",
      "\n",
      "         1.0       0.50      0.45      0.48        11\n",
      "         2.0       0.80      0.73      0.76        11\n",
      "         3.0       0.71      0.86      0.77        14\n",
      "         4.0       1.00      0.80      0.89         5\n",
      "\n",
      "    accuracy                           0.71        41\n",
      "   macro avg       0.75      0.71      0.73        41\n",
      "weighted avg       0.71      0.71      0.70        41\n",
      "\n"
     ]
    }
   ],
   "source": [
    "from sklearn.metrics import classification_report\n",
    "\n",
    "# 打印分类报告\n",
    "report = classification_report(label_te, pre_te)\n",
    "print('分类报告为：\\n', report)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
